package com.example.train.business.controller;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.train.business.req.ConfirmOrderDoReq;
import com.example.train.business.service.BeforeConfirmOrderService;
import com.example.train.business.service.ConfirmOrderService;
import com.example.train.common.exception.BusinessExceptionEnum;
import com.example.train.common.resp.CommonResp;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/confirm-order")
public class ConfirmOrderController {

    private static final Logger LOG = LoggerFactory.getLogger(ConfirmOrderController.class);

    @Resource
    private BeforeConfirmOrderService beforeConfirmOrderService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private ConfirmOrderService confirmOrderService;

    @Value("${spring.profiles.active}")
    private String env;

    @SentinelResource(value = "confirmOrderDo",blockHandler = "doConfirmBlock")
    @PostMapping("/do")
    public CommonResp<Object> doConfirm(@Valid @RequestBody ConfirmOrderDoReq req) {
        //开发环境不校验验证码
        if(!env.equals("dev")){
            // 图形验证码校验
            String imageCodeToken= req.getImageCodeToken();
            String imageCode = req.getImageCode();
            String imageCodeRedis=stringRedisTemplate.opsForValue().get(imageCodeToken);
            LOG.info("从redis中获取到的验证码：{}", imageCodeRedis);
            if(ObjUtil.isEmpty(imageCodeRedis)){
                return new CommonResp<>(false ,"验证码已过期", null);
            }
            // 验证码校验，忽略大小写
            if(!imageCodeRedis.equalsIgnoreCase(imageCode)){
                return new CommonResp<>(false, "验证码错误", null);
            } else {
                // 验证通过后，移除验证码
                stringRedisTemplate.delete(imageCodeToken);
            }
        }

        Long id=beforeConfirmOrderService.beforeDoConfirm(req);
        return new CommonResp<>(String.valueOf(id));
    }

    @GetMapping("/query-line-count/{id}")
    public CommonResp<Integer> queryLineCount(@PathVariable Long id) {
        Integer count=confirmOrderService.queryLineCount(id);
        return new CommonResp<>(count);
    }

    @GetMapping("/cancel/{id}")
    public CommonResp<Integer> cancel(@PathVariable Long id) {
        Integer count = confirmOrderService.cancel(id);
        return new CommonResp<>(count);
    }

    /** 降级方法，需包含限流方法的所有参数和BlockException参数，且返回值要保持一致
     * @param req
     * @param e
     */
    public CommonResp<Object> doConfirmBlock(ConfirmOrderDoReq req, BlockException e) {
        LOG.info("ConfirmOrderController购票请求被限流：{}", req);
        CommonResp<Object> commonResp = new CommonResp<>();
        commonResp.setSuccess(false);
        commonResp.setMessage(BusinessExceptionEnum.CONFIRM_ORDER_FLOW_EXCEPTION.getDesc());
        return commonResp;
    }
}
